﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlServerCe;
using Scan.PDAService;
using System.Windows.Forms;
using Scan.DAO;

namespace Scan
{
    class ArchivesWarehouses
    {
        private static void DeleteWarehouses()
        {
            SQLiteHelper.ExecuteNonQuery("Delete from Warehouses", null);
        }

        //下载数据
        public static bool DownloadWarehouses()
        {
            //执行 WebService 方法
            ResponseBaseOfWarehouse response = GlobalCache.Instance.WebServiceIfe.GetWarehouses(false, "", "");

            if (response.ErrorInfo.ErrorCode == 1)
            {
                if (response.Lists != null || response.Lists.Length != 0)
                {
                    DeleteWarehouses();

                    string sql = "insert into Warehouses (cWhCode, cWhName, cRemark) values (@cWhCode, @cWhName, @cRemark)";
                    object[] parameters = new object[3];

                    // 循环插入用户数据
                    foreach (Warehouse Warehouse in response.Lists)
                    {
                        parameters[0] = Warehouse.CWhCode;
                        parameters[1] = Warehouse.CWhName;
                        parameters[2] = Warehouse.CBarCode;
                        SQLiteHelper.ExecuteNonQuery(sql, parameters);
                    }
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取仓库档案
        /// </summary>
        /// <param name="CompCode">仓库编码</param>
        /// <param name="CompName">仓库名称</param>
        public static Warehouse[] GetWarehouses(Boolean Fuzzyed, string WhCode, string WhName)
        {
            if (Configuration.Instance.OnLineType == OnLineType.Online)
            {   // 在线方式

                //执行 WebService 方法
                
                ResponseBaseOfWarehouse response = GlobalCache.Instance.WebServiceIfe.GetWarehouses(Fuzzyed, WhCode, WhName);
                if (response.ErrorInfo.ErrorCode == 1)
                {
                    if (response.Lists != null || response.Lists.Length != 0)
                    {
                        return response.Lists;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    MessageBox.Show(response.ErrorInfo.ErrorMsg, "获取仓库档案出错！");
                    return null;
                }
            }
            else
            {   // 离线方式
                string sSQL = "select cWhCode, cWhName, cRemark from Warehouses";
                return null;
            }
        }
    }
}
